#include <stdlib.h>
#include <string.h>
#include <linux/limits.h>
#include <stdio.h>
#include "cJSON.h"
#include "vqc.h"

trans_side_t *trans_side_list[3];
trans_t *trans_list[RTSIG_MAX];
capacitor_t *capacitor_list[RTSIG_MAX];
svg_t *svg_list[RTSIG_MAX];
reactors_t *reactors_list[RTSIG_MAX];


bus_t *bus_new() {
    return calloc(1, sizeof(bus_t));
}

trans_t *trans_new() {
    return calloc(1, sizeof(trans_t));
}

trans_side_t *trans_side_new() {
    return calloc(1, sizeof(trans_side_t));
}

capacitor_t *capacitor_new() {
    return calloc(1, sizeof(capacitor_t));
}

reactors_t *reactors_new() {
    return calloc(1, sizeof(reactors_t));
}

svg_t *svgs_new() {
    return calloc(1, sizeof(svg_t));
}

void bus_free(bus_t *ptr) {
    if (!ptr) return;

    free(ptr);
}

static void parse_trans_side(const cJSON *json, trans_side_t *ptr) {
    for (cJSON *next = json; next; next = next->next) {
        for (int i = 0; i < cJSON_GetArraySize(next); i++) {
            cJSON *cJson = cJSON_GetArrayItem(json, i);
            if (!strcmp("sideName", cJson->string)) {
                ptr->side_name = cJSON_GetStringValue(cJson);
            } else if (!strcmp("id", cJson->string)) {
                ptr->id = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("yxSwitch1", cJson->string)) {
                ptr->yx_switch1 = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("switch1", cJson->string)) {
                ptr->switch1 = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("yxSwitch2", cJson->string)) {
                ptr->yx_switch2 = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("switch2", cJson->string)) {
                ptr->switch2 = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("ycReactivePower", cJson->string)) {
                ptr->yc_reactive_factor = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("reactivePower", cJson->string)) {
                ptr->reactive_power = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("ycPowerFactor", cJson->string)) {
                ptr->yc_power_factor = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("powerFactor", cJson->string)) {
                ptr->power_factor = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("reserve0", cJson->string)) {
                ptr->reserve0 = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("reserve1", cJson->string)) {
                ptr->reserve1 = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("reserve2", cJson->string)) {
                ptr->reserve2 = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("reserve3", cJson->string)) {
                ptr->reserve3 = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("reserve4", cJson->string)) {
                ptr->reserve4 = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("reserve5", cJson->string)) {
                ptr->reserve5 = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("reserve6", cJson->string)) {
                ptr->reserve6 = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("reserve7", cJson->string)) {
                ptr->reserve7 = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("reserve8", cJson->string)) {
                ptr->reserve8 = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("reserve9", cJson->string)) {
                ptr->reserve9 = cJSON_GetNumberValue(cJson);
            }
        }
    }
}

static void parse_trans(const cJSON *json, trans_t *ptr) {
    for (cJSON *next = json; next; next = next->next) {
        for (int i = 0; i < cJSON_GetArraySize(next); i++) {
            cJSON *cJson = cJSON_GetArrayItem(json, i);
            if (!strcmp("transName", cJson->string)) {
                ptr->trans_name = cJSON_GetStringValue(cJson);
            } else if (!strcmp("id", cJson->string)) {
                ptr->id = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("quantity", cJson->string)) {
                ptr->quantity = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("level", cJson->string)) {
                ptr->level = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("levelLink", cJson->string)) {
                ptr->level_link = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("ycLevel", cJson->string)) {
                ptr->yc_level = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("ykUp", cJson->string)) {
                ptr->yk_up = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("ykDown", cJson->string)) {
                ptr->yk_down = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("upLink", cJson->string)) {
                ptr->up_link = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("downLink", cJson->string)) {
                ptr->down_link = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("yxStatus", cJson->string)) {
                ptr->yx_status = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("statusLink", cJson->string)) {
                ptr->status_link = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("transSides", cJson->string)) {
                //获取trans_side结构体数组个数
                int num = cJSON_GetArraySize(cJson);
                for (int j = 0; j < num; j++) {
                    cJSON *trans_side_item = cJSON_GetArrayItem(cJson, j);
                    trans_side_t *trans_side = trans_side_new();
                    parse_trans_side(trans_side_item, trans_side);
                    trans_side_list[j] = trans_side;
                }
                ptr->trans_side_size = num;
                ptr->trans_side = trans_side_list;
            } else if (!strcmp("reserve0", cJson->string)) {
                ptr->reserve0 = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("reserve1", cJson->string)) {
                ptr->reserve1 = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("reserve2", cJson->string)) {
                ptr->reserve2 = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("reserve3", cJson->string)) {
                ptr->reserve3 = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("reserve4", cJson->string)) {
                ptr->reserve4 = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("reserve5", cJson->string)) {
                ptr->reserve5 = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("reserve6", cJson->string)) {
                ptr->reserve6 = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("reserve7", cJson->string)) {
                ptr->reserve7 = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("reserve8", cJson->string)) {
                ptr->reserve8 = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("reserve9", cJson->string)) {
                ptr->reserve9 = cJSON_GetNumberValue(cJson);
            }
        }
    }
}

static void parse_capacitor(const cJSON *json, capacitor_t *ptr) {
    for (cJSON *next = json; next; next = next->next) {
        for (int i = 0; i < cJSON_GetArraySize(next); i++) {
            cJSON *cJson = cJSON_GetArrayItem(json, i);
            if (!strcmp("capacitorName", cJson->string)) {
                ptr->capacitor_name = cJSON_GetStringValue(cJson);
            } else if (!strcmp("id", cJson->string)) {
                ptr->id = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("quantity", cJson->string)) {
                ptr->quantity = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("yxStatus", cJson->string)) {
                ptr->yx_status = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("statusLink", cJson->string)) {
                ptr->status_link = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("yxVqc", cJson->string)) {
                ptr->yx_vqc = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("vqcLink", cJson->string)) {
                ptr->vqc_link = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("ykSwitch", cJson->string)) {
                ptr->yk_switch = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("switchLink", cJson->string)) {
                ptr->switch_link = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("reserve0", cJson->string)) {
                ptr->reserve0 = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("reserve1", cJson->string)) {
                ptr->reserve1 = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("reserve2", cJson->string)) {
                ptr->reserve2 = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("reserve3", cJson->string)) {
                ptr->reserve3 = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("reserve4", cJson->string)) {
                ptr->reserve4 = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("reserve5", cJson->string)) {
                ptr->reserve5 = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("reserve6", cJson->string)) {
                ptr->reserve6 = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("reserve7", cJson->string)) {
                ptr->reserve7 = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("reserve8", cJson->string)) {
                ptr->reserve8 = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("reserve9", cJson->string)) {
                ptr->reserve9 = cJSON_GetNumberValue(cJson);
            }
        }
    }
}

static void parse_reactors(cJSON *json, reactors_t *ptr) {
    for (cJSON *next = json; next; next = next->next) {
        for (int i = 0; i < cJSON_GetArraySize(next); i++) {
            cJSON *cJson = cJSON_GetArrayItem(json, i);
            if (!strcmp("reactorName", cJson->string)) {
                ptr->reactor_name = cJSON_GetStringValue(cJson);
            } else if (!strcmp("id", cJson->string)) {
                ptr->id = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("quantity", cJson->string)) {
                ptr->quantity = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("yxStatus", cJson->string)) {
                ptr->yx_status = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("statusLink", cJson->string)) {
                ptr->status_link = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("yxFault", cJson->string)) {
                ptr->yx_fault = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("faultLink", cJson->string)) {
                ptr->fault_link = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("ykSwitch", cJson->string)) {
                ptr->yk_switch = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("switchLink", cJson->string)) {
                ptr->switch_link = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("reserve0", cJson->string)) {
                ptr->reserve0 = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("reserve1", cJson->string)) {
                ptr->reserve1 = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("reserve2", cJson->string)) {
                ptr->reserve2 = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("reserve3", cJson->string)) {
                ptr->reserve3 = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("reserve4", cJson->string)) {
                ptr->reserve4 = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("reserve5", cJson->string)) {
                ptr->reserve5 = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("reserve6", cJson->string)) {
                ptr->reserve6 = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("reserve7", cJson->string)) {
                ptr->reserve7 = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("reserve8", cJson->string)) {
                ptr->reserve8 = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("reserve9", cJson->string)) {
                ptr->reserve9 = cJSON_GetNumberValue(cJson);
            }
        }
    }
}

static void parse_svgs(const cJSON *json, svg_t *ptr) {
    for (cJSON *next = json; next; next = next->next) {
        for (int i = 0; i < cJSON_GetArraySize(next); i++) {
            cJSON *cJson = cJSON_GetArrayItem(json, i);
            if (!strcmp("svgName", cJson->string)) {
                ptr->svg_name = cJSON_GetStringValue(cJson);
            } else if (!strcmp("id", cJson->string)) {
                ptr->id = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("quantity", cJson->string)) {
                ptr->quantity = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("svgKp", cJson->string)) {
                ptr->svg_kp = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("yxStatus", cJson->string)) {
                ptr->yx_status = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("statusLink", cJson->string)) {
                ptr->status_link = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("yxRun", cJson->string)) {
                ptr->yx_run = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("runLink", cJson->string)) {
                ptr->run_link = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("yxAtresia", cJson->string)) {
                ptr->yx_atresia = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("atresiaLink", cJson->string)) {
                ptr->artresia_link = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("ycReactivePower", cJson->string)) {
                ptr->yc_reactive_power = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("reactivePower", cJson->string)) {
                ptr->reactive_power = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("ytIdle", cJson->string)) {
                ptr->yt_idle = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("idleLink", cJson->string)) {
                ptr->idle_link = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("reserve0", cJson->string)) {
                ptr->reserve0 = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("reserve1", cJson->string)) {
                ptr->reserve1 = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("reserve2", cJson->string)) {
                ptr->reserve2 = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("reserve3", cJson->string)) {
                ptr->reserve3 = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("reserve4", cJson->string)) {
                ptr->reserve4 = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("reserve5", cJson->string)) {
                ptr->reserve5 = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("reserve6", cJson->string)) {
                ptr->reserve6 = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("reserve7", cJson->string)) {
                ptr->reserve7 = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("reserve8", cJson->string)) {
                ptr->reserve8 = cJSON_GetNumberValue(cJson);
            } else if (!strcmp("reserve9", cJson->string)) {
                ptr->reserve9 = cJSON_GetNumberValue(cJson);
            }
        }
    }
}

int parse_json(const cJSON *json, bus_t *ptr) {
    if (NULL == json) return -1;
    cJSON *item = cJSON_GetArrayItem(json, 0);
    for (cJSON *next = item; next; next = next->next) {
        if (!strcmp("busName", next->string)) {
            ptr->bus_name = cJSON_GetStringValue(next);
        } else if (!strcmp("id", next->string)) {
            ptr->id = cJSON_GetNumberValue(next);
        } else if (!strcmp("voltageLevel", next->string)) {
            ptr->voltage_level = cJSON_GetStringValue(next);
        } else if (!strcmp("upLine", next->string)) {
            ptr->up_line = cJSON_GetNumberValue(next);
        } else if (!strcmp("downLine", next->string)) {
            ptr->down_line = cJSON_GetNumberValue(next);
        } else if (!strcmp("ycVoltage", next->string)) {
            ptr->yc_voltage = cJSON_GetNumberValue(next);
        } else if (!strcmp("isUse", next->string)) {
            ptr->is_use = cJSON_GetStringValue(next);
        } else if (!strcmp("trans", next->string)) {
            //获取trans结构体数组个数
            int num = cJSON_GetArraySize(next);
            for (int j = 0; j < num; j++) {
                cJSON *trans_item = cJSON_GetArrayItem(next, j);
                trans_t *trans = trans_new();
                parse_trans(trans_item, trans);
                trans_list[j] = trans;
            }
            ptr->trans_size = num;
            ptr->trans = trans_list;
        } else if (!strcmp("capacitors", next->string)) {
            //获取capacitor结构体数组个数
            int num = cJSON_GetArraySize(next);
            for (int j = 0; j < num; j++) {
                cJSON *capacitor_item = cJSON_GetArrayItem(next, j);
                capacitor_t *capacitor = capacitor_new();
                parse_capacitor(capacitor_item, capacitor);
                capacitor_list[j] = capacitor;
            }
            ptr->capacitor_size = num;
            ptr->capacitor = capacitor_list;
        } else if (!strcmp("reactors", next->string)) {
            //获取reactor结构体数组个数
            int num = cJSON_GetArraySize(next);
            for (int j = 0; j < num; j++) {
                cJSON *reactors_item = cJSON_GetArrayItem(next, j);
                reactors_t *reactors = reactors_new();
                parse_reactors(reactors_item, reactors);
                reactors_list[j] = reactors;
            }
            ptr->reactor_size = num;
            ptr->reactors = reactors_list;
        } else if (!strcmp("svgs", next->string)) {
            //获取reactor结构体数组个数
            int num = cJSON_GetArraySize(next);
            for (int j = 0; j < num; j++) {
                cJSON *svg_item = cJSON_GetArrayItem(next, j);
                svg_t *svg = svgs_new();
                parse_svgs(svg_item, svg);
                svg_list[j] = svg;
            }
            ptr->svg_size = num;
            ptr->svg = svg_list;
        } else if (!strcmp("reserve0", next->string)) {
            ptr->reserve0 = cJSON_GetNumberValue(next);
        } else if (!strcmp("reserve1", next->string)) {
            ptr->reserve1 = cJSON_GetNumberValue(next);
        } else if (!strcmp("reserve2", next->string)) {
            ptr->reserve2 = cJSON_GetNumberValue(next);
        } else if (!strcmp("reserve3", next->string)) {
            ptr->reserve3 = cJSON_GetNumberValue(next);
        } else if (!strcmp("reserve4", next->string)) {
            ptr->reserve4 = cJSON_GetNumberValue(next);
        } else if (!strcmp("reserve5", next->string)) {
            ptr->reserve5 = cJSON_GetNumberValue(next);
        } else if (!strcmp("reserve6", next->string)) {
            ptr->reserve6 = cJSON_GetNumberValue(next);
        } else if (!strcmp("reserve7", next->string)) {
            ptr->reserve7 = cJSON_GetNumberValue(next);
        } else if (!strcmp("reserve8", next->string)) {
            ptr->reserve8 = cJSON_GetNumberValue(next);
        } else if (!strcmp("reserve9", next->string)) {
            ptr->reserve9 = cJSON_GetNumberValue(next);
        }
    }
    return 0;
}

int print_json(cJSON *json, bus_t *bus) {
    if (NULL == bus) {
        return 0;
    }
    cJSON_AddStringToObject(json, "busName", bus->bus_name);
    cJSON_AddNumberToObject(json, "id", bus->id);
    cJSON_AddStringToObject(json, "voltageLevel", bus->voltage_level);
    cJSON_AddBoolToObject(json, "isUse", bus->is_use);
    cJSON_AddNumberToObject(json, "upLine", bus->up_line);
    cJSON_AddNumberToObject(json, "downLine", bus->down_line);
    cJSON_AddNumberToObject(json, "voltageLink", bus->voltage_link);
    cJSON_AddNumberToObject(json, "ycVoltage", bus->yc_voltage);
    cJSON_AddNumberToObject(json, "reserve0", bus->reserve0);
    cJSON_AddNumberToObject(json, "reserve1", bus->reserve1);
    cJSON_AddNumberToObject(json, "reserve2", bus->reserve2);
    cJSON_AddNumberToObject(json, "reserve3", bus->reserve3);
    cJSON_AddNumberToObject(json, "reserve4", bus->reserve4);
    cJSON_AddNumberToObject(json, "reserve5", bus->reserve5);
    cJSON_AddNumberToObject(json, "reserve6", bus->reserve6);
    cJSON_AddNumberToObject(json, "reserve7", bus->reserve7);
    cJSON_AddNumberToObject(json, "reserve8", bus->reserve8);
    cJSON_AddNumberToObject(json, "reserve9", bus->reserve9);

    /*trans部分*/
    cJSON *trans = cJSON_AddArrayToObject(json, "trans");
    trans_t **trans_l = bus->trans;
    if (NULL != trans_l[0]) {
        for (int i = 0; i < bus->trans_size; i++) {
            cJSON *tran = cJSON_CreateObject();
            cJSON_AddStringToObject(tran, "transName", trans_l[i]->trans_name);
            cJSON_AddNumberToObject(tran, "id", trans_l[i]->id);
            cJSON_AddNumberToObject(tran, "quantity", trans_l[i]->quantity);
            cJSON_AddNumberToObject(tran, "level", trans_l[i]->level);
            cJSON_AddNumberToObject(tran, "levelLink", trans_l[i]->level_link);
            cJSON_AddNumberToObject(tran, "ycLevel", trans_l[i]->yc_level);
            cJSON_AddNumberToObject(tran, "ykUp", trans_l[i]->yk_up);
            cJSON_AddNumberToObject(tran, "upLink", trans_l[i]->up_link);
            cJSON_AddNumberToObject(tran, "ykDown", trans_l[i]->yk_down);
            cJSON_AddNumberToObject(tran, "downLink", trans_l[i]->down_link);
            cJSON_AddNumberToObject(tran, "yxStatus", trans_l[i]->yx_status);
            cJSON_AddNumberToObject(tran, "statusLink", trans_l[i]->status_link);
            cJSON_AddNumberToObject(tran, "reserve0", trans_l[i]->reserve0);
            cJSON_AddNumberToObject(tran, "reserve1", trans_l[i]->reserve1);
            cJSON_AddNumberToObject(tran, "reserve2", trans_l[i]->reserve2);
            cJSON_AddNumberToObject(tran, "reserve3", trans_l[i]->reserve3);
            cJSON_AddNumberToObject(tran, "reserve4", trans_l[i]->reserve4);
            cJSON_AddNumberToObject(tran, "reserve5", trans_l[i]->reserve5);
            cJSON_AddNumberToObject(tran, "reserve6", trans_l[i]->reserve6);
            cJSON_AddNumberToObject(tran, "reserve7", trans_l[i]->reserve7);
            cJSON_AddNumberToObject(tran, "reserve8", trans_l[i]->reserve8);
            cJSON_AddNumberToObject(tran, "reserve9", trans_l[i]->reserve9);
            /*transSide部分*/
            cJSON *trans_sides = cJSON_AddArrayToObject(tran, "transSides");
            trans_side_t **trans_side_l = trans_l[i]->trans_side;
            if (NULL != trans_side_l[0]) {
                for (int j = 0; j < trans_l[i]->trans_side_size; j++) {
                    cJSON *trans_side = cJSON_CreateObject();
                    cJSON_AddStringToObject(trans_side, "sideName", trans_side_l[j]->side_name);
                    cJSON_AddNumberToObject(trans_side, "id", trans_side_l[j]->id);
                    cJSON_AddNumberToObject(trans_side, "yxSwitch1", trans_side_l[j]->yx_switch1);
                    cJSON_AddNumberToObject(trans_side, "switch1", trans_side_l[j]->switch1);
                    cJSON_AddNumberToObject(trans_side, "yxSwitch2", trans_side_l[j]->yx_switch2);
                    cJSON_AddNumberToObject(trans_side, "switch2", trans_side_l[j]->switch2);
                    cJSON_AddNumberToObject(trans_side, "ycReactivePower", trans_side_l[j]->yc_reactive_factor);
                    cJSON_AddNumberToObject(trans_side, "reactivePower", trans_side_l[j]->reactive_power);
                    cJSON_AddNumberToObject(trans_side, "ycPowerFactor", trans_side_l[j]->yc_power_factor);
                    cJSON_AddNumberToObject(trans_side, "powerFactor", trans_side_l[j]->power_factor);
                    cJSON_AddNumberToObject(trans_side, "reserve0", trans_side_l[i]->reserve0);
                    cJSON_AddNumberToObject(trans_side, "reserve1", trans_side_l[i]->reserve1);
                    cJSON_AddNumberToObject(trans_side, "reserve2", trans_side_l[i]->reserve2);
                    cJSON_AddNumberToObject(trans_side, "reserve3", trans_side_l[i]->reserve3);
                    cJSON_AddNumberToObject(trans_side, "reserve4", trans_side_l[i]->reserve4);
                    cJSON_AddNumberToObject(trans_side, "reserve5", trans_side_l[i]->reserve5);
                    cJSON_AddNumberToObject(trans_side, "reserve6", trans_side_l[i]->reserve6);
                    cJSON_AddNumberToObject(trans_side, "reserve7", trans_side_l[i]->reserve7);
                    cJSON_AddNumberToObject(trans_side, "reserve8", trans_side_l[i]->reserve8);
                    cJSON_AddNumberToObject(trans_side, "reserve9", trans_side_l[i]->reserve9);
                    cJSON_AddItemToArray(trans_sides, trans_side);
                }
            }
            cJSON_AddItemToArray(trans, tran);
        }
    }
    /*capacitors部分*/
    cJSON *capacitors = cJSON_AddArrayToObject(json, "capacitors");
    capacitor_t **capacitor_l = bus->capacitor;
    if (NULL != capacitor_l[0]) {
        for (int i = 0; i < bus->capacitor_size; i++) {
            cJSON *capacitor = cJSON_CreateObject();
            cJSON_AddStringToObject(capacitor, "capacitorName", capacitor_l[i]->capacitor_name);
            cJSON_AddNumberToObject(capacitor, "id", capacitor_l[i]->id);
            cJSON_AddNumberToObject(capacitor, "quantity", capacitor_l[i]->quantity);
            cJSON_AddNumberToObject(capacitor, "yxStatus", capacitor_l[i]->yx_status);
            cJSON_AddNumberToObject(capacitor, "statusLink", capacitor_l[i]->status_link);
            cJSON_AddNumberToObject(capacitor, "yxVqc", capacitor_l[i]->yx_vqc);
            cJSON_AddNumberToObject(capacitor, "vqcLink", capacitor_l[i]->vqc_link);
            cJSON_AddNumberToObject(capacitor, "ykSwitch", capacitor_l[i]->yk_switch);
            cJSON_AddNumberToObject(capacitor, "switchLink", capacitor_l[i]->switch_link);
            cJSON_AddNumberToObject(capacitor, "reserve0", capacitor_l[i]->reserve0);
            cJSON_AddNumberToObject(capacitor, "reserve1", capacitor_l[i]->reserve1);
            cJSON_AddNumberToObject(capacitor, "reserve2", capacitor_l[i]->reserve2);
            cJSON_AddNumberToObject(capacitor, "reserve3", capacitor_l[i]->reserve3);
            cJSON_AddNumberToObject(capacitor, "reserve4", capacitor_l[i]->reserve4);
            cJSON_AddNumberToObject(capacitor, "reserve5", capacitor_l[i]->reserve5);
            cJSON_AddNumberToObject(capacitor, "reserve6", capacitor_l[i]->reserve6);
            cJSON_AddNumberToObject(capacitor, "reserve7", capacitor_l[i]->reserve7);
            cJSON_AddNumberToObject(capacitor, "reserve8", capacitor_l[i]->reserve8);
            cJSON_AddNumberToObject(capacitor, "reserve9", capacitor_l[i]->reserve9);
            cJSON_AddItemToArray(capacitors, capacitor);
        }
    }

    /*reactors部分*/
    cJSON *reactors = cJSON_AddArrayToObject(json, "reactors");
    reactors_t **reactors_l = bus->reactors;
    if (NULL != reactors_l[0]) {
        for (int i = 0; i < bus->reactor_size; i++) {
            cJSON *reactor = cJSON_CreateObject();
            cJSON_AddStringToObject(reactor, "reactorName", reactors_l[i]->reactor_name);
            cJSON_AddNumberToObject(reactor, "id", reactors_l[i]->id);
            cJSON_AddNumberToObject(reactor, "quantity", reactors_l[i]->quantity);
            cJSON_AddNumberToObject(reactor, "yxStatus", reactors_l[i]->yx_status);
            cJSON_AddNumberToObject(reactor, "statusLink", reactors_l[i]->status_link);
            cJSON_AddNumberToObject(reactor, "yxFault", reactors_l[i]->yx_fault);
            cJSON_AddNumberToObject(reactor, "faultLink", reactors_l[i]->fault_link);
            cJSON_AddNumberToObject(reactor, "ykSwitch", reactors_l[i]->yk_switch);
            cJSON_AddNumberToObject(reactor, "switchLink", reactors_l[i]->switch_link);
            cJSON_AddNumberToObject(reactor, "reserve0", reactors_l[i]->reserve0);
            cJSON_AddNumberToObject(reactor, "reserve1", reactors_l[i]->reserve1);
            cJSON_AddNumberToObject(reactor, "reserve2", reactors_l[i]->reserve2);
            cJSON_AddNumberToObject(reactor, "reserve3", reactors_l[i]->reserve3);
            cJSON_AddNumberToObject(reactor, "reserve4", reactors_l[i]->reserve4);
            cJSON_AddNumberToObject(reactor, "reserve5", reactors_l[i]->reserve5);
            cJSON_AddNumberToObject(reactor, "reserve6", reactors_l[i]->reserve6);
            cJSON_AddNumberToObject(reactor, "reserve7", reactors_l[i]->reserve7);
            cJSON_AddNumberToObject(reactor, "reserve8", reactors_l[i]->reserve8);
            cJSON_AddNumberToObject(reactor, "reserve9", reactors_l[i]->reserve9);
            cJSON_AddItemToArray(reactors, reactor);
        }
    }

    /*svg部分*/
    cJSON *svgs = cJSON_AddArrayToObject(json, "svgs");
    svg_t **svg_l = bus->svg;
    if (NULL != svg_l[0]) {
        for (int i = 0; i < bus->svg_size; i++) {
            cJSON *svg = cJSON_CreateObject();
            cJSON_AddStringToObject(svg, "svgName", svg_l[i]->svg_name);
            cJSON_AddNumberToObject(svg, "id", svg_l[i]->id);
            cJSON_AddNumberToObject(svg, "quantity", svg_l[i]->quantity);
            cJSON_AddNumberToObject(svg, "svgKp", svg_l[i]->svg_kp);
            cJSON_AddNumberToObject(svg, "yxStatus", svg_l[i]->yx_status);
            cJSON_AddNumberToObject(svg, "statusLink", svg_l[i]->status_link);
            cJSON_AddNumberToObject(svg, "yxRun", svg_l[i]->yx_run);
            cJSON_AddNumberToObject(svg, "runLink", svg_l[i]->run_link);
            cJSON_AddNumberToObject(svg, "yxAtresia", svg_l[i]->yx_atresia);
            cJSON_AddNumberToObject(svg, "atresiaLink", svg_l[i]->artresia_link);
            cJSON_AddNumberToObject(svg, "ycReactivePower", svg_l[i]->yc_reactive_power);
            cJSON_AddNumberToObject(svg, "reactivePower", svg_l[i]->reactive_power);
            cJSON_AddNumberToObject(svg, "ytIdle", svg_l[i]->yt_idle);
            cJSON_AddNumberToObject(svg, "idleLink", svg_l[i]->idle_link);
            cJSON_AddNumberToObject(svg, "reserve0", svg_l[i]->reserve0);
            cJSON_AddNumberToObject(svg, "reserve1", svg_l[i]->reserve1);
            cJSON_AddNumberToObject(svg, "reserve2", svg_l[i]->reserve2);
            cJSON_AddNumberToObject(svg, "reserve3", svg_l[i]->reserve3);
            cJSON_AddNumberToObject(svg, "reserve4", svg_l[i]->reserve4);
            cJSON_AddNumberToObject(svg, "reserve5", svg_l[i]->reserve5);
            cJSON_AddNumberToObject(svg, "reserve6", svg_l[i]->reserve6);
            cJSON_AddNumberToObject(svg, "reserve7", svg_l[i]->reserve7);
            cJSON_AddNumberToObject(svg, "reserve8", svg_l[i]->reserve8);
            cJSON_AddNumberToObject(svg, "reserve9", svg_l[i]->reserve9);
            cJSON_AddItemToArray(svgs, svg);
        }
    }
}